package com.founder.helloworldapp;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.IBinder;

public class LocationService extends Service {

	private GPSManager manager;
	
	private LocationBinder binder;
	
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		binder = new LocationBinder();
		manager = new GPSManager(this);
		manager.setListener(binder);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		manager.setListener(null);
		manager.stop();
	}
	
	public class LocationBinder extends Binder implements GPSManager.GPSLocationListener {

		private GPSManager.GPSLocationListener listener;
		
		public void setListener(GPSManager.GPSLocationListener listener) {
			this.listener = listener;
		}
		
		public void update(Location newLocation, Location oldLocation) {
			if (listener != null) {
				listener.update(newLocation, oldLocation);
			}
		}
		
		public void start() {
			manager.start();
		}
		
		public void stop() {
			manager.stop();
		}
		
	}
}
